//
//  CDPDatePicker.m
//  CDPDatePicker
//
//  Created by MAC on 15/3/30.
//  Copyright (c) 2015年 com.xuezi.CDP. All rights reserved.
//

#import "CDPDatePicker.h"

@implementation CDPDatePicker
{
    NSInteger type;
}

-(id)initWithView:(UIView *)view delegate:(id<CDPDatePickerDelegate>)delegate{
    if (self=[super init]) {
        _view=view;
        _delegate=delegate;
        _theTypeOfDatePicker=3;
        
        
        _maskView = [[UIView alloc] initWithFrame:_view.frame];
        _maskView.backgroundColor = [UIColor blackColor];
        _maskView.alpha = 0;
        [_maskView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(popDatePicker)]];
        [_view addSubview:_maskView];

        
        _datePickerView=[[UIView alloc] initWithFrame:CGRectMake(0,_view.frame.size.height,_view.frame.size.width,256)];
        _datePickerView.backgroundColor=ECCOLOR(0, 0, 0, .8f);
        [_view addSubview:_datePickerView];
        
        
        _datePicker=[[UIDatePicker alloc]init];
        _datePicker.frame = CGRectMake(0,40,_datePickerView.frame.size.width,216);
        _datePicker.backgroundColor = [UIColor whiteColor];
        _datePicker.datePickerMode = UIDatePickerModeDateAndTime;
        [_datePickerView addSubview:_datePicker];
        
        _dateConfirmButton=[[UIButton alloc] initWithFrame:CGRectMake(_datePickerView.frame.size.width/2,0,_datePickerView.frame.size.width/2,40)];
        [_dateConfirmButton setTitle:@"确定" forState:UIControlStateNormal];
        _dateConfirmButton.titleLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:15];
        _dateConfirmButton.userInteractionEnabled=YES;
        _dateConfirmButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _dateConfirmButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
        [_dateConfirmButton addTarget:self action:@selector(dateConfirmClick) forControlEvents:UIControlEventTouchUpInside];
        [_dateConfirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        _dateConfirmButton.backgroundColor=[UIColor lightGrayColor];
        [_datePickerView addSubview:_dateConfirmButton];

        
        _dateCancleButton=[[UIButton alloc] initWithFrame:CGRectMake(0,0,_datePickerView.frame.size.width/2,40)];
        [_dateCancleButton setTitle:@"取消" forState:UIControlStateNormal];
        _dateCancleButton.titleLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:15];
        _dateCancleButton.userInteractionEnabled=YES;
        _dateCancleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _dateCancleButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        [_dateCancleButton addTarget:self action:@selector(popDatePicker) forControlEvents:UIControlEventTouchUpInside];
        [_dateCancleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        _dateCancleButton.backgroundColor=[UIColor lightGrayColor];
        [_datePickerView addSubview:_dateCancleButton];
}
    
    return self;
}


//确定选择
-(void)dateConfirmClick{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    if (type == 1) {
        [formatter setDateFormat:@"HH:mm"];
    }else if (type == 2){
        [formatter setDateFormat:@"yyyy/MM/dd"];
    }else{
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    }
    NSString *string=[formatter stringFromDate:[_datePicker date]];
    
    [self.delegate CDPDatePickerDidConfirm:string];
    
    [self popDatePicker];
    _datePicker.date =[NSDate date];
}


//是否可选择以前的时间
-(void)setIsBeforeTime:(BOOL)isBeforeTime WithData:(NSDate *)date{
    if (isBeforeTime==NO) {
        [_datePicker setMinimumDate:date];
        _datePicker.date = date;
    }
    else{
        [_datePicker setMinimumDate:[NSDate dateWithTimeIntervalSince1970:0]];
        _datePicker.date = date;
    }
}


//datePicker显示类别
-(void)setTheTypeOfDatePicker:(NSInteger)theTypeOfDatePicker{
    type  = theTypeOfDatePicker;
    if (theTypeOfDatePicker==1) {
        //只显示时间
        _datePicker.datePickerMode = UIDatePickerModeTime;
    }
    else if(theTypeOfDatePicker==2){
        //只显示日期
        _datePicker.datePickerMode = UIDatePickerModeDate;
    }
    else if(theTypeOfDatePicker==3){
        //时间与日期都显示
        _datePicker.datePickerMode = UIDatePickerModeDateAndTime;
    }
    else{
        NSLog(@"时间类别选择错误");
    }
}


#pragma mark pickerView动画效果
//出现
-(void)pushDatePicker{
    [UIView animateWithDuration:0.3 animations:^{
        _maskView.alpha = 0.3;
        _datePickerView.frame=CGRectMake(0,_view.frame.size.height-256,_view.frame.size.width,256);
    }];
}


//消失
-(void)popDatePicker{
    [UIView animateWithDuration:0.3 animations:^{
        _maskView.alpha = 0;
        _datePickerView.frame=CGRectMake(0,_view.frame.size.height,_view.frame.size.width,256);
    }];
}


-(void)dealloc{
    self.delegate=nil;
}



@end
